<?php

namespace App\Http\Controllers;

use App\Http\Controllers\JwtController;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;

class UserController extends Controller
{
    // 获取token
    public function getToken(Request $request)
    {
        $appkey = $request->input('appkey');
        if (!$appkey) {
            return response()->json([
                'code' => 201,
                'msg' => 'user_id不能为空!',
            ]);
        }
        $appkey = JwtController::getToken(['appkey' => $appkey]);
        return response()->json([
            'code' => 200,
            'msg' => 'ok',
            'appkey' => $appkey,
        ]);
    }

    //用户注册
    public function regster(Request $request)
    {
        $name = $request->input('name');
        $pwd = $request->input('pwd');
        $v_code = $request->input('code'); //验证码
        $key = $request->input('key'); //key
        if (!$v_code) {
            return response()->json([
                'code' => 201,
                'msg' => '验证码为Null',
            ]);
        }
        if (captcha_api_check($v_code, $key)) {
            $Users = new User();
            $err = $Users->where('name', $name)->get();
            if (count($err) > 0) {
                return response()->json([
                    'code' => 201,
                    'msg' => '用户名被占用!',
                ]);
            }
            $id = uniqid('justwen');
            $id2 = uniqid();
            $Users->name = $name;
            $Users->pwd = $pwd;
            $Users->appkey = $id . $id2;
            $Users->viptime = Time() + 3600 * 24 * 15; //免费15天
            $res = $Users->save();
            if ($res) {
                return response()->json([
                    'code' => 200,
                    'msg' => '注册成功',
                    'data' => $res,

                ]);
            } else {
                return response()->json([
                    'code' => 201,
                    'msg' => '注册失败',
                    'data' => $res,

                ]);
            }
        } else {
            return response()->json([
                'code' => 201,
                'v_code' => $v_code,
                'msg' => '验证码无效',

            ]);
        }

    }
    // 用户登录
    public function login(Request $request)
    {
        $name = $request->input('name');
        $pwd = $request->input('pwd');
        $Users = new User();
        $res = $Users->where('name', $name)
            ->where('pwd', $pwd)->get();
        if (count($res) == 0) {
            return response()->json([
                'code' => 201,
                'msg' => '账号或密码错误',

            ]);
        } else {
            return response()->json([
                'code' => 200,
                'msg' => 'ok',
                'data' => $res[0],

            ]);
        }

    }

    //文件上传接收
    public function UploadOneServlet(Request $request)
    {
        // $filepath = dirname(dirname(dirname(dirname(__FILE__)))) . '\public\img';
        $filepath = base_path() . '\public\img';
        $filename = "abc2.jpg";
        if ($request->file('photo')->isValid()) {
            $request->file('photo')->move($filepath, $filename);
            return response()->json([
                'code' => 200,
                'path' => $filepath,
                'msg' => 'ok',
            ]);
        } else {

            return response()->json([
                'code' => 201,
                'msg' => 'err',
            ]);
        }

    }

    //验证token
    public function verifyToken(Request $request)
    {
        $token = $request->input('token');
        if (!$token) {
            return response()->json([
                'code' => 201,
                'msg' => 'token不能为空!',
            ]);
        }
        $verify = JwtController::verifyToken($token);
        if (!$verify) {
            return response()->json([
                'code' => 201,
                'msg' => '签证错误！',
            ]);
        } else {
            return response()->json([
                'code' => 200,
                'msg' => 'ok',
                'verify' => $verify,
            ]);
        }

    }

    //发送邮箱
    public function setEail(Request $request)
    {

        Mail::raw('你好，我是PHP程序！', function ($message) {
            $to = '2422142078@qq.com';
            $message->to($to)->subject('你好啊！美女');
        });
        // 返回的一个错误数组，利用此可以判断是否发送成功
        dd(Mail::failures());

    }

    public function updete(Request $request)
    {
        $db = DB::table('user');
        $res = $db->where('id', 2)
            ->update(['name' => 'lin']);
        return response()->json([
            'code' => 200,
            'msg' => 'ok',
            'data' => $res,
        ]);
    }

    public function info(Request $request)
    {

        return response()->json([
            'code' => 200,
            'msg' => 'ok',
            'data' => 'data',
        ]);
    }

    public function getNumcode(Request $request)
    {
        $code = rand(1000, 9999);
        $key = JustwenCode::getCode(['code' => $code]);
        return response()->json([
            'code' => 200,
            'msg' => 'ok',
            'numcode' => $code,
            'key' => $key,
        ]);

    }

    public function verifyNumcode(Request $request)
    {
        $code = $request->input('code');
        $key = $request->input('key');
        $verify = JustwenCode::verifyCode($key);
        if ($verify) {
            if ($verify == $code) {
                return response()->json([
                    'code' => 200,
                    'msg' => 'ok',
                    'data' => $verify,
                ]);
            }

        } else {
            return response()->json([
                'code' => 201,
                'msg' => 'err',
                'data' => false,
            ]);
        }

    }

}
